﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using KPIS.GERP.GBMS.BLL;
using KPIS.GERP.GBMS.IBLL;
using KPIS.GERP.GBMS.MODEL;
using Telerik.Web.UI;

namespace KPIS.GERP.GBMS.APP_UI
{
    public enum ComboBoxMode
    {
        SelectAllMode, SelectTitleMode, SelectNone
    }

    public partial class SourceCombo : System.Web.UI.UserControl
    {
        //public int val = 0;
        public RadComboBox ctrChild;
        private ISourceBLL src = new SourceBLL();
        private IExpenseGroupBLL expGrp = new ExpenseGroupBLL();
        private SsMng createby = new SsMng();

        protected void Page_Load(object sender, EventArgs e)
        {
            cbSource.AutoPostBack = !(ctrChild == null);
        }

        public void LoadSourceComboBox(ComboBoxMode mode)
        {
            LoadSourceComboBox(0, "", "", "", 0, 0, mode);
        }

        public void LoadSourceComboBox(int seq, string code, string name, string descr, int parent, int type, ComboBoxMode mode)
        {
            cbSource.DataSource = src.Search(seq, code, name, descr, parent, type);
            cbSource.DataBind();

            if (mode == ComboBoxMode.SelectAllMode)
            {
                cbSource.Items.Insert(0, new RadComboBoxItem("<--- ทั้งหมด --->", ""));
            }
            else if (mode == ComboBoxMode.SelectTitleMode)
            {
                cbSource.Items.Insert(0, new RadComboBoxItem("- เลือกแหล่งเงิน -", ""));
            }
        }

        protected void cbSource_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            int val = 0;

            if (cbSource.SelectedIndex > 0)
            {
                val = Convert.ToInt32(cbSource.SelectedValue);

                if (ctrChild != null)
                {
                    ctrChild.DataSource = expGrp.Search(0, "", "", "", 0, val);
                    ctrChild.DataBind();

                    ctrChild.Items.Insert(0, new RadComboBoxItem(cbSource.Items[0].Text));
                    ctrChild.Focus();
                }
            }
            else
                if (ctrChild != null)
                    ctrChild.Items.Clear();
        }
    }
}